export function generateNumber(start: number, end: number) {
    return Math.floor(Math.random() * (end - start + 1)) + start
}
export function generateNumberArray(minNumber: number, maxNumber: number) {
    var numberArray = [];
    for (var i = minNumber; i <= maxNumber; i++) {
        numberArray.push(i);
    }

    return numberArray;
}


export function getRandomPoint(_size: number) {
    var x = (generateNumber(0, window.innerWidth - _size));
    var y = (generateNumber(0, window.innerHeight - _size));
    return { x: x, y: y };
}

export function renderE(size: number, direction: string, position: { x: number, y: number }, { context, canvas }: { context: CanvasRenderingContext2D, canvas: HTMLCanvasElement }) {
    context && context.clearRect(0, 0, canvas.width, canvas.height);

    const { x, y } = position || getRandomPoint(size);

    context.fillStyle = 'black';  // 设置填充颜色为黑色
    let border = size / 5;

    if (direction == '右') {
        context.fillRect(x, y, size, border);
        let line2 = y + border * 2
        context.fillRect(x, line2, size, border);
        let line3 = line2 + border * 2
        context.fillRect(x, line3, size, border);
        context.fillRect(x, y, border, size);
    }
    if (direction == '左') {
        context.fillRect(x, y, size, border);
        let line2 = y + border * 2
        context.fillRect(x, line2, size, border);
        let line3 = line2 + border * 2
        context.fillRect(x, line3, size, border);
        context.fillRect(x + size - border, y, border, size);
    }
    if (direction == '上') {
        context.fillRect(x, y, border, size);
        let line2 = x + border * 2
        context.fillRect(line2, y, border, size);
        let line3 = line2 + border * 2
        context.fillRect(line3, y, border, size);
        context.fillRect(x, y + size - border, size, border);
    }
    if (direction == '下') {
        context.fillRect(x, y, border, size);
        let line2 = x + border * 2
        context.fillRect(line2, y, border, size);
        let line3 = line2 + border * 2
        context.fillRect(line3, y, border, size);
        context.fillRect(x, y, size, border);
    }
}
export function generateDireaction() {
    let _ = generateNumber(1, 4);
    return (['', '上', '下', '左', '右'])[_]
}

export enum ButtonEnum {
    A = 0,
    B = 1,
    X = 2,
    Y = 3,
    L1 = 4,
    R1 = 5,
    L2 = 6,
    R2 = 7,
    SELECT = 8,
    START = 9,
    TOP = 12,
    BOTTOM = 13,
    LEFT = 14,
    RIGHT = 15,
    CIRCLEL = 10,
    CIRCLER = 11,

}
export function mat2gray(_frequency: number, _direction: number, _opacity: number, { context, canvas }: { context: CanvasRenderingContext2D, canvas: HTMLCanvasElement }) {
    // context.clearRect(0, 0, canvas.width, canvas.height);
    // 遍历矩阵中的每个元素，并将其绘制为灰度  
    for (let i = 0; i < 512; i++) {
        for (let j = 0; j < 512; j++) {
            // 将矩阵元素的值映射到0-255的范围内，然后转换为十六进制表示  
            // const gray = Math.round(mat[i][j] * 255);  
            const gray = Math.round(128 + 127 * Math.cos(j / _frequency * 2 * Math.PI + Math.PI / 4));
            // console.log(mat[i][j],gray, 'gray')
            context.fillStyle = `rgba(${gray}, ${gray}, ${gray}, ${_opacity})`;
            if (_direction === 1) {
                context.fillRect(i, j, 1, 1);
            } else {
                context.fillRect(j, i, 1, 1);
            }
        }
    }
    setTimeout(() => {
        context.clearRect(0, 0, canvas.width, canvas.height);
    }, 250);
}

function gaborFunction(x: number, y: number, f: number, theta: number, sigma: number, gamma: number, phase: number, { centerX, centerY }: { centerX: number, centerY: number }) {
    let xp = (x - centerX) * Math.cos(theta) + (y - centerY) * Math.sin(theta);
    let yp = -(x - centerX) * Math.sin(theta) + (y - centerY) * Math.cos(theta);
    let exponent = -((xp * xp + gamma * gamma * yp * yp) / (2 * sigma * sigma));
    let cosTerm = Math.cos(2 * Math.PI * f * xp + theta + phase);
    return Math.exp(exponent) * cosTerm;
}

function applyGaborFilter(x: number, y: number, f: number, theta: number, sigma: number, gamma: number, phase: number, _baseColor: number, { context, centerX, centerY }: { context: CanvasRenderingContext2D, centerX: number, centerY: number }) {
    const gray = Math.round(_baseColor + 127 * gaborFunction(x, y, f, theta, sigma, gamma, phase, { centerX, centerY }));
    context.fillStyle = `rgba(${gray}, ${gray}, ${gray})`;
    context.fillRect(x, y, 1, 1);
}

export function mat2grayGabor(_frequency: number, _orientation: number, _baseColor: number, { canvas, context, centerX, centerY }: { context: CanvasRenderingContext2D, canvas: HTMLCanvasElement, centerX: number, centerY: number }) {
console.log(arguments, 'arguments')
    const gamma = 1;               // 椭圆比例因子
    const phase = [0, Math.PI / 2 , Math.PI, Math.PI / 2 + Math.PI][generateNumber(1,4)-1] as number; // 调整相位
    const _sigma = 100;
    for (let i = 0; i < canvas.width; i++) {
        for (let j = 0; j < canvas.height; j++) {
            applyGaborFilter(i, j, _frequency, _orientation, _sigma, gamma, phase, _baseColor, { context, centerX, centerY });
        }
    }
    setTimeout(() => {
        context.clearRect(0, 0, canvas.width, canvas.height);
    }, 200)
}

export  function generateWeightedRandomNumber(minNumber:number, maxNumber:number, type : 'asc'| 'desc' = 'asc') {
    var totalWeight = (maxNumber + minNumber) * (maxNumber - minNumber + 1) / 2;
    // 生成随机数
    var randomValue = Math.random();
    // 根据随机值确定生成的数字
    var cumulativeWeight = 0;
    for (var i = minNumber; i <= maxNumber; i++) {
      cumulativeWeight = type == 'asc' ? cumulativeWeight + (i) : cumulativeWeight + (maxNumber + minNumber - i);
      if (randomValue <= cumulativeWeight / totalWeight) {
        return i;
      }
    }
    return maxNumber; // 默认返回最大的数字（这种情况应该不会发生）
  }